package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.ServeQuotaAdapter;
import com.eeepay.eeepay_v2.model.LimitInfo;
import com.eeepay.eeepay_v2.util.BaseGrpcTask;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.v2_library.log.LogUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.TitleBar;

import java.util.ArrayList;
import java.util.List;

import cn.eeepay.api.grpc.nano.ProductApiServiceGrpc;
import cn.eeepay.api.grpc.nano.ProductSerProto;
import cn.eeepay.api.grpc.nano.ReqBaseProto;
import io.grpc.ManagedChannel;

/**
 * Created by zw on 2016/3/14.
 * 服务限额界面
 */
public class ServeQuotaActivity extends ABBaseActivity implements TitleBar.LeftBtnOnClickListener, View.OnClickListener {

    private TitleBar titleBar;
    private ListView limitLv;
    private Button comBtn;

    private ServeQuotaAdapter adapter;
    private String agentId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_service_limit;
    }

    @Override
    protected void initWidget() {
        if(bundle != null){
            agentId = bundle.getString(Constant.AGENT_ID);
            LogUtils.d("TAG", "agent_id = " + agentId);
        }
        titleBar = getViewById(R.id.tb_service_limit);
        limitLv = getViewById(R.id.lv_service_limit);
        comBtn = getViewById(R.id.btn_limit_complete);
        comBtn.setVisibility(View.GONE);
    }

    @Override
    protected void eventOnClick() {

        titleBar.setLeftOnClickListener(this);
        comBtn.setOnClickListener(this);
        adapter = new ServeQuotaAdapter(this);
        limitLv.setAdapter(adapter);
        if(TextUtils.isEmpty(agentId)){
            showToast("代理商ID为空");
        }else {
            new GrpcTask().execute(Constant.URL.URL, Constant.URL.PORT);
        }
    }

    @Override
    public void onClick(View v) {
    }

    @Override
    public void onLeftClick(View view) {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    private class GrpcTask extends BaseGrpcTask<ProductSerProto.AgentQuotaApiList> {

        @Override
        protected ProductSerProto.AgentQuotaApiList doInBack(ManagedChannel channel) {
            ProductApiServiceGrpc.ProductApiServiceBlockingStub stub = ProductApiServiceGrpc.newBlockingStub(channel);
            ReqBaseProto.ReqOneStr request = new ReqBaseProto.ReqOneStr();
            request.param = agentId;
            ProductSerProto.AgentQuotaApiList response = stub.getAgentQuotaList(request);
            return response;
        }

        @Override
        protected void updateUI(ProductSerProto.AgentQuotaApiList result) {
            if(result == null) {
                showToast("查询失败");
                return;
            }
            if(result.resStatus.status){
                ProductSerProto.AgentQuotaApi[] list = result.quotaList;
                if(list.length > 0){
                    List<LimitInfo> datas = new ArrayList<>();
                    for (int i = 0; i < list.length; i++){
                        LimitInfo limit = new LimitInfo();
                        limit.setServiceName(list[i].serviceName);
                        limit.setCardType(list[i].cardType);
                        limit.setServiceTime(list[i].holidaysMark);
                        limit.setServiceId(list[i].serviceId);
                        limit.setFixedQuota("1");
                        limit.setId(list[i].id + "");
                        limit.setMinAmount(list[i].singleMinAmount + "");
                        limit.setCountAmount(list[i].singleCountAmount + "");
                        limit.setDayAmount(list[i].singleDayAmount + "");
                        limit.setDayCardAmount(list[i].singleDaycardAmount + "");
                        limit.setDayCardConut(list[i].singleDaycardCount + "");
                        limit.setCardType(list[i].cardType);
                        limit.setServiceTime(list[i].holidaysMark);
                        datas.add(limit);
                    }
                    adapter.setList(datas);
                }
            }
        }
    }

}
